package com.record.controller;

import com.record.dto.FormulaIngredientDto;
import com.record.entity.Formula;
import com.record.service.FormulaService;
import com.record.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.record.utils.Empties.impty;

@Api(tags = "查询功效")
@RestController
public class FormulaController {

    @Autowired
    private FormulaService formulaService;

    /**
     * 通过方剂名查询完整信息（组方+剂量+功效）
     * 访问示例: http://localhost:8888/formula
     */
    @ApiOperation(value = "根据方剂查组方以及功效")
    @GetMapping("/getByName")
    public Result getFormulaByName(@RequestParam String name) {
        Formula formula = formulaService.getFormulaByName(name);
        if (formula == null) {
            return Result.fail("未找到该方剂");
        }
        return Result.success(formula);
    }



    @ApiOperation(value = "通过组方(药材名)查询方剂名称")
    @GetMapping("/getByIngredient")
    public Result getByIngredient(@RequestParam String ingredientName) {
        if (impty(ingredientName)) {
            return Result.fail("药材名不能为空");
        }
        List<FormulaIngredientDto> rows = formulaService.getByIngredient(ingredientName.trim());
        return impty(rows) ? Result.fail("未找到含该药材的方剂：" + ingredientName)
                : Result.success(rows);
    }







}
